home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 32 / leastq.zip / LEASTQ.DOC < prev    next >
Text File  |  1990-02-21  |  17KB  |  270 lines

  1.    LEASTQ is a general purpose curve fitting program designed to find the 
  2.  coefficients a[i] of a function, y(x), of the form: 
  3.  
  4.  (1)  y(x) = a[0] + a[1]*f1(x) + a[2]*f2(x) + . . . .
  5.  
  6.  where fi(x) are some functions of x. Data for this program consists of a 
  7.  set of points, each one described by three numbers, x[k], y[k] and err[k], 
  8.  where y[k] is an experimental value measured at x[k] and err[k] is the 
  9.  uncertainty in this measurement. The program finds the coefficients a[i] 
  10.  that minimize, Chisq, the sum of the squares of the deviations of the 
  11.  calculated y from the measured y. 
  12.  
  13.  (2)  Chisq = Sum(k = 1;Npts) of  (y[k] -ycalc(x[k]))^2/err[k]^2
  14.  
  15.  where ycalc(x[k]) is y calculated from x[k] with formula (1) above. The 
  16.  procedure of minimizing Chisq is called least squares fitting. 
  17.  
  18.     To call this program, type LEASTQ [MYFILE.DAT] [/E] <CR> from the command 
  19.  line. The parameters in square brackets are optional. MYFILE.DAT is the 
  20.  generic name of the file containing the data you want to fit; if this 
  21.  parameter is omitted, the opening screen of the program will show you a 
  22.  list of all the files in your current directory with the extension DAT
  23.  and you can select one of these files by choosing its number. The '/E'
  24.  parameter is used to force the program to use EGA rather than VGA graphics.
  25.  Otherwise the program will use VGA graphics if it detects that your machine 
  26.  has VGA available. The reason for this option is that you may want to run 
  27.  LEASTQ with a graphics screen dump TSR loaded. Some of these, such as 
  28.  SCRNDU, do not handle VGA graphics properly.
  29.  
  30.     LEASTQ assumes that any file with extension 'DAT' is a data file for this 
  31.  program. If you have other files with this extension on the disk where you 
  32.  keep LEASTQ, you may want to consider moving them to another disk or 
  33.  directory so that you don't call them by mistake. 
  34.  
  35.     A data file for this program consists of three columns of numbers. The 
  36.  first column contains the values of x, the second column the correspond-
  37.  ing values of y, and the third column the errror in y. Each line in the 
  38.  data file, therefore, corresponds to a data point. The proper format can be 
  39.  ascertained from the sample data files that have been provided. Spacing of 
  40.  the columns is not critical and comments placed after the third column are 
  41.  ignored by the program. You can generate a data file with an editor or word 
  42.  processor, or a data file could be generated by an application program. You 
  43.  can also generate a data file from LEASTQ. Start the program without 
  44.  specifying a data file in the command line and choose 'none of the above'
  45.  from the menu of .DAT files in your directory. You will be put into an 
  46.  editor that will allow you to enter data from the keyboard which you can 
  47.  subsequently save to the disk if you wish. 
  48.  
  49.     When you enter the keyboard routine, you will be asked if your data has 
  50.  evenly spaced values of x; if so, enter the first x and the step 
  51.  between the x's. The program will then fill in the values of x for you. 
  52.  This can save a lot of time. If you enter the values of x yourself, you can 
  53.  put your data in any order; LEASTQ will sort your data in order of 
  54.  ascending x when you are finished. It is perfectly acceptable for two 
  55.  points to have the same x coordinate; this sometimes happens with real data 
  56.  when you go back and repeat a measurement. 
  57.  
  58.     You will then be asked if the dependent variable, y, is a count of 
  59.  something: the number of beans in a jar, for instance. In that case, the 
  60.  program will calculate the appropriate value of the error, which is the 
  61.  square root of y. If you define the data as a count, you will not be 
  62.  allowed to enter negative values of y. If you want all data points to have 
  63.  the same error, enter the first error and then just <cr> for subsequent 
  64.  entries; the error for the current point will be taken from the previous 
  65.  point. All data points MUST have a finite error; any data point without an 
  66.  error will be ignored by the program. This is true, also, of data read from 
  67.  a file; points with zero error will be ignored. 
  68.  
  69.     You can use the cursor keys to go back and correct data that was 
  70.  entered incorrectly.
  71.   
  72.     Hit <esc> to exit the data entry routine and return to the main program. 
  73.  You must have at least 3 valid data points to run this program and you are 
  74.  allowed a maximum of 1024 data points. You will exit automatically if you 
  75.  exceed this number. The limit applies also to data read from a file. if the 
  76.  program is reading data from a file with more than 1024 entries, it will 
  77.  exit after the 1024th data point. 
  78.  
  79.     It is difficult to enter a large number of data points without making a 
  80.  few mistakes - forgetting a decimal point, for instance. When you have 
  81.  finished entering data from the keyboard, you will be shown a plot of your 
  82.  data. If you see some obvious mistakes, press <N> at the question 'Data 
  83.  OK?' . Count the points from the left to find the point you want to change; 
  84.  the first point on the left is #1. Enter the number of the point; it will be 
  85.  highlighted in blinking white and you will be asked to confirm that it is 
  86.  the point you want to change. You will be asked to replace x, y and err for 
  87.  the point you have chosen. Entering <CR> leaves the quantity unchanged. If 
  88.  there are additional points you would like to correct. press <Y> at the 
  89.  question 'more'; press <N> when you are finished correcting your data. Then 
  90.  you will be given the option of storing your data as a file for later 
  91.  use. Note that you do not give your file name the extension 'DAT'; the 
  92.  program will do that for you. A data file will be created in your current 
  93.  directory with the name you have given. 
  94.  
  95.     After you have loaded your data from a file or from the keyboard, you 
  96.  are shown a menu of functions that can be used to fit the data. A library 
  97.  of commonly used functions has been provided. Select the function you wish 
  98.  to use by typing the highlighted letter. The appropriate function is often 
  99.  determined by the differential equation you want to solve and the boundary 
  100.  conditions. In cases where the choice of function is open, it is sometimes 
  101.  useful to look at a plot of the data before selecting the species of 
  102.  function to use. Selecting 'V' (for 'View') from the menu will display a 
  103.  plot of the data together with the list of functions, and your choice can 
  104.  be made while viewing this plot; otherwise, hitting any key will return to 
  105.  the select functions menu. 
  106.  
  107.     After you have selected your function, you will be asked to supply a 
  108.  range over which x is to vary. For example, if your data consists of the 
  109.  Dow Jones Industrial Average, y, versus the year, x, over the range 1948 to 
  110.  1988, you would probably want x to run over a range from 0 to 40 or perhaps 
  111.  0 to 1, rather than 1948 to 1988, so that the coefficients, a[i] will have 
  112.  a reasonable size. Note that if you choose Powers and a range of 0 to 1.0, 
  113.  all coefficients will have the same weight in the calculated value of y at 
  114.  the maximum x; you can see at a glance if the a[i] are converging to 0 as i 
  115.  increases. For trigonometric functions, you will be asked the number of 
  116.  cycles that the range of x is to represent. Eliot wave theorists can play 
  117.  around with those Dow Jones averages. For Chebychev and Legendre 
  118.  polynomials the range of x should be -1.0 to +1.0 or smaller. For Bessel 
  119.  functions the lower limit must be greater or equal 0, and the upper limit 
  120.  should be chosen no greater than 25 because of the limitations in the 
  121.  algorithm used in this program to calculate Bessel functions. 
  122.  
  123.     Next you will be asked the number of coefficients in the fit. The 
  124.  minimum allowable number is 2, the largest number is 12 with the restriction 
  125.  that the number of coefficients must be less than the number of data 
  126.  points. After each calculation the program will display a plot of the 
  127.  data fitted to the curve calculated by equation (1), the Chisq of the 
  128.  fit as calculated from equation (2) (the lower the Chisq, the better 
  129.  the fit) and ask you 'accept fit?'. If you think more coefficients are 
  130.  needed, answer 'N'. It is good practice to start with a small number of 
  131.  coefficients and increase this number until adding another coefficient 
  132.  produces only a small decrease in Chisq. The algorithm for calculating 
  133.  the a[i] involves a matrix inversion. With a large number of 
  134.  coefficient this inversion sometimes bombs; the bomb will place you 
  135.  back in the menu at the place where you choose the number of 
  136.  coefficients and you will just have to settle for a smaller number of 
  137.  coefficients. Sometimes increasing the range of x will permit you to 
  138.  use more coefficients. 
  139.  
  140.     In two special cases, function species 'Exponen' and 'Gaussian', you are 
  141.  allowed two and three coefficients respectively; the step of choosing the 
  142.  number of coefficients is bypassed. 
  143.  
  144.     If you answer 'y' to accept the fit, the program will list the 
  145.  coefficients, a[i], and a comparison of experimental y[k] with calculated 
  146.  y's for all data points, the chisq of your fit and a quantity called the 
  147.  confidence level, which will be discussed below. The program will then 
  148.  present you with a menu of options. Choose 'D' to try a new data file. 
  149.  Choose 'R' to try a different range in x. Choose 'S' if you want to try a 
  150.  different species of function. For instance, you may have fit the data to 
  151.  Sines and now notice that the coefficients of sin(2*x), sin(4*x) etc. are 
  152.  very small and have large errors; you might then try fitting with Oddsines. 
  153.  The most general fit to trigonometric functions is called Fourier; you can 
  154.  start with this and later choose the species of trigonometric function that 
  155.  gives the best fit to your data. 
  156.  
  157.      Sometimes it becomes evident that your fit is not working out; the 
  158.  Chisq remains large or the matrix inversion bombs when you try to use an 
  159.  adequate number of coefficients. In this case, it is best to pretend to 
  160.  'buy' the solution so as to get into a menu where you can pick a different 
  161.  species of function or a different range in x. The characteristics of a 
  162.  good fit are that the coefficients are reasonably small and converging: 
  163.  that is a[i+1] > a[i] and that the errors are small, at least for the first 
  164.  few terms. 
  165.  
  166.     Finally, if your inability to get a good fit appears to be the result of 
  167.  just one or two bad points, you can edit the data so as to bring the 
  168.  troublemakers in line. Choose option 'F' for 'Fudge data'. You will get a 
  169.  plot of your data together with your most recent fit from which you can 
  170.  adjust points with the same procedure used to correct bad points after 
  171.  entering them from the keyboard. Points which have been fudged are 
  172.  henceforth plotted in green, whereas the original unchanged data points are 
  173.  plotted in red. You realize, of course, that using this procedure with 
  174.  actual experimental data is highly unethical, so you will have to answer to 
  175.  your conscience if you use this option. Because LEASTQ does not want to 
  176.  become your accomplice in crime, it will not allow you to save your 
  177.  shamelessly doctored data to disk. 
  178.  
  179.  Choose option 'Q' to quit the program. The program will ask you if you want 
  180.  a printed output of your results. Hit 'Y' to get a printed record of your
  181.  fit togother with a comparison of your data with the calculated values.
  182.  
  183.  Some data files have been provided for demonstration purposes:
  184.  
  185.  SCURVE   Try fitting to Powers. Note that Chisq does not decrease much 
  186.           after 4 coefficients. Also try Bessels with a range of 0 to 1.0.
  187.  
  188.  SQUAREWV Try Sines with range = 1.0 cycles. Then, since the even values 
  189.           obviously aren't pulling their weight in the boat, try Oddsines. 
  190.           Note that this data does not represent a true square wave; the 
  191.           sides have a finite slope. A good fit to a true square wave 
  192.           requires more terms than this program allows. 
  193.  
  194.  SAWTOOTH Try Sines with range = 1.0 cycles.  Note that with 8 coefficients 
  195.           the stupid computer thinks that it has done a fantastic job of 
  196.           fitting the data. You, the clever human being, have to set it 
  197.           straight. Six coefficients is about the best fit.
  198.  
  199.  TRAPEZD  Try Powers with a range from 0 to 1. Note that it takes many 
  200.           coefficients to get a good fit and that the coefficients, a[i], 
  201.           are very big and have big errors. Powerseries is not a good choice for 
  202.           extrapolation; this expression will explode outside the calculated 
  203.           range. Then try Oddsines with range = 0.5 cycles. 
  204.  
  205.  EXPONEN  Obviously, the first function to try is Exponen. Set the range from 
  206.           0 to 60 so that you get the real decay length as coefficient a[1].
  207.           You will get a logarithmic plot which gives you a good view of the
  208.           points with small y but a poor view of the points with large y. 
  209.           Then try Powers which will give you an arithmetic plot and a good 
  210.           view of the big points. Powers with 6 coefficients gives a good fit.
  211.  
  212.  GAUSSIAN: Try Gauss, another special case. The log of the data is fit to a
  213.           parabola with only two coefficients allowed and the plot is 
  214.           logarithmic. Then try Oddsines with range = 0.5 cycles to see an
  215.           arithmetic plot. 
  216.  
  217.  THERMOCP Data for this were taken from the Handbook of Chemistry and Physics
  218.           with very small errors assigned. Try Powers with a range of x from 
  219.           0 to 27.39. (Since you intend to use the calculated coefficients in a 
  220.           programable pocket calculator, you use the real range of x so you 
  221.           can enter the coefficients directly.) The plot is not much use 
  222.           here; the errors are too small to see. Keep an eye on Chisq and 
  223.           keep adding terms until a decent fit is obtained. 6 coefficients 
  224.           are sufficient to give < 0.1 degree accuracy over the range of x. 
  225.           The first coefficient, a[0], is negligible and should be thrown 
  226.           away. Try also Tscheby (Chebychev Polynomials) with range 0 to 1. 
  227.  
  228.      For most of these data files the errors were chosen arbitrarily; thus, 
  229.  the absolute value of chisq has no particular significance and is merely a 
  230.  relative indicator of goodness of fit. For data from actual experimental 
  231.  measurements where a reliable estimate of the uncertainty is available, the 
  232.  value of Chisq is significant. A rough rule of thumb is that chisq should be 
  233.  somewhat smaller than the number of Degrees of Freedom. A more precise 
  234.  evaluation of the significance of the Chisq obtained is the Confidence 
  235.  Level which is calculated every time you 'buy' a fit. The confidence level 
  236.  is a number that varies between 0 and 1. A perfect fit corresponds to a 
  237.  confidence level of 1.000. An extremely poor fit is described by a 
  238.  confidence level of 0.000. In practice, any confidence level over 0.8 is 
  239.  pretty good fit; with a confidence level under 0.5 you had better think 
  240.  about framing a new hypothesis, i.e., choosing a different species of 
  241.  function or range in x. When choosing between two species of functions, the 
  242.  confidence level rather than the value of chisq is the best indicator of 
  243.  goodness of fit. 
  244.  
  245.     For example, consider once again the data file GAUSSIAN, which was 
  246.  created with a random number generator and statistical errors. Using 
  247.  function species Gaussian you will get a chisq of 23.839 for 31 degrees of 
  248.  freedom giving a confidence level of 0.8171. Now try species Oddsines with 
  249.  12 coefficients. You will get a chisq of 19.31, which looks like a better 
  250.  fit, but with only 22 degrees of freedom your confidence level is 0.6259. 
  251.  What is happening is that with 12 coefficients to play around with LEASTQ 
  252.  can get cute and adjust to the noise in the data. Oddsines with only 6 
  253.  coefficients is a better fit giving a confidence level of 0.7533.
  254.  
  255.  While the higher the confidence level, the better the fit, you should be 
  256.  suspicious of experimental data with too high a confidence level; it may 
  257.  indicate that some unscrupulous person has Fudged the data. Try fudging 
  258.  GAUSSIAN. With just a few changed points you can get a CL of 0.998.
  259.  
  260.                ***************************************
  261.  
  262.  This program is free and may be distributed by bulletin boards and archiving 
  263.  services at their usual rates. Report bugs and suggestions for improvement 
  264.  to the author:
  265.                    John D. Fox
  266.                    309 NW 24th Street
  267.                    Gainesville, FL 32607
  268.                    CIS: 71270,2304
  269.  
  270.